<?php
// vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldclose=all foldmethod=marker:
/**
 * 项目：   5anet
 * 文件：   class/calendar/ShowSmall.class.php
 *
 * 显示小日历
 *
 * PHP Version 5
 *
 * @package:   class.calendar
 * @author:    Mike.G Chinese Name: 黄叶 <hylinux@gmail.com>
 * @license:   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1 
 * @copyright: www.5anet.com
 * @version:   $Id: $
 * @date:      $Date: $
 */

include_once CLASS_PATH.'main/BaseAction.class.php';

include_once CLASS_PATH.'calendar/CalendarUtil.class.php';


class ShowSmall extends BaseAction {

   /**
    * 数据库的连接
    */
   private $db;


   /**
    * 构造函数
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function __construct() {
      $this->db = $this->getDB();
   }


   /**
    * 显示日历首页
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function run() {

      $now = getdate();

      $now_year = $now['year'];
      $now_month = $now['mon'];
      $now_day = $now['mday'];

      //取得日期的参数
      $year = $this->getParameter('year');

      if ( !$year ) {
         $year = $now_year;
      }

      $month = $this->getParameter('mon');
      if ( !$month ) {
         $month = $now_month;
      }

      $day = $this->getParameter('day');
      if ( !$day ) {
         $day = $now_day;
      }

      //如果提供的日期不合法，则采用现在的日期

      if ( !checkdate($month, $day, $year) ) {
         $year = $now_year;
         $month = $now_month;
         $day = $now_day;
      }


      //生成日历

      $total_day = 0;

      if ( $month == 1 ||
            $month == 3 ||
            $month == 5 ||
            $month == 7 ||
            $month == 8 ||
            $month == 10 ||
            $month == 12  ) {
               $total_day = 31;
      } else if ( $month == 2 ) {
         if ( checkdate(2, 29, $year) ) {
            $total_day = 29;
         } else {
            $total_day = 28;
         }
      } else {
         $total_day = 30;
      }




      //计算1号星期几
      //和本月最后一天星期几
      $temp_date = getdate(mktime(0, 0, 0, $month, 1, $year));
      $begin_week = $temp_date['wday'];
      $temp_date = getdate(mktime(0, 0, 0, $month, $total_day, $year));
      $end_week = $temp_date['wday'];

      $split_before_day = $begin_week - 0;
      $split_end_day = 6 - $end_week;


      $total_days = $total_day + $split_before_day + $split_end_day;

      $total_line = ceil( $total_days / 7 );

      $my_html = "";

      //输出日历的行数
      $now_day = 1;

      $my_html = "<table  cellpadding=\"1\" cellspacing=\"1\" border=\"0\" align=\"center\" ".
          " width=\"95%\" bgcolor=\"#c5c5b3\">\n";

      for ( $i = 1; $i <=$total_line; $i++ ) {

         $my_html .= "<tr bgcolor=\"#f6f7f1\" height=\"20\">\n";

         for($j=0; $j<7; $j++) {
            if ( $j < $begin_week && $i==1 ) {
               $my_html .= "<td align=\"center\" valign=\"middle\" >\n";
               $my_html .= "&nbsp;</td>\n";
            } else if ( $j > $end_week && $i == $total_line ) {
               $my_html .= "<td align=\"center\" valign=\"middle\">\n";
               $my_html .= "&nbsp;</td>\n";
            } else if ( $day == $now_day ) {
               //求得所有的事件记录
               $now_time = mktime(0, 0, 0, $month, $now_day, $year);

               $my_html .= "<td align=\"center\" valign=\"middle\" bgcolor=\"#c2c3b5\">\n";
               $my_html .= "<a href='#thisclick' OnClick=\"ShowDayData($now_time, 1);\" ".
                  " title=\"显示当天事件及计划\">".
                  "$now_day</a>\n";
               $my_html .= "</td>\n";
               $now_day = $now_day + 1;

            } else {
               //求得所有的事件记录
               $now_time = mktime(0, 0, 0, $month, $now_day, $year);

               $my_html .= "<td align=\"center\" valign=\"middle\" >\n";
               $my_html .= "<a href='#thisclick' OnClick=\"ShowDayData($now_time, 1);\" ".
                  " title=\"显示当天事件及计划\">".
                  "$now_day</a>\n";

               $my_html .= "</td>\n";
               $now_day = $now_day + 1;

            }

         }

         $my_html .= "</tr>\n";
      }

      $my_html .= "</table>\n";

      header('Content-type: text/html;charset=utf-8');
      echo $my_html;

   }
}


?>
